<?php
namespace Home\Model;
use Think\Model;
use Org\Net\IpLocation;
class UserOperalogModel extends Model
{
    
    
    protected $_auto = array (
        array('createdate','date',1,'function',array('Y-m-d H:i:s')), // 对create_time字段在插入的时候写入当前时间
    );
     protected $_validate = array(
        array('uid','require','用户ID不能为空！'), //默认情况下用正则进行验证
        array('operation',array(1,2,3),'操作类型值的范围不正确！',1,'in'), // 1=登录 2=注销 3=签到
        array('client_type',array(1,2),'客户端类型值范围不正确！',1,'in'), // 1=电脑登录 2=移动端登录
    );
    
     protected $_link = array(
         'basicLog'=>array(
             'mapping_type'    =>HAS_ONE,
             'class_name'    =>'BasicLog',
             'foreign_key'=>'userId',
             'as_fields'=>'email,nickname',
         ),
     );
     
     
     /**
      * 插入操作记录
      * @param unknown $arry
      * @return number|unknown
      */
	public function inst_userOperaLogs($arry){
	    foreach ($arry as $key => $value){
	        $data[$key] = $value;
	    };
	    if($data["operation"] == 1){
	        $operName = "登录";
	    }elseif ($data["operation"] == 2){
	        $operName = "注销";
	    }elseif ($data["operation"] == 3){
	        $operName = "签到";
	    };
	    $ipAndAre = D('Mess','Event')->getIPaddress();
	    foreach ($ipAndAre as $key => $value){
	        $data[$key] = $value;
	    };
	    $result["status"]=0;
	    if (!$this->create($data)){
	        $result["mess"]=$this->getError();
	    }else{
	        $result = $this->add(); // 写入数据到数据库 
            if($result){
                $result["status"]=1;
                $result["mess"]=$result;
            }
	    };
	    return $result;
	}
	/**
	 * 条件查询操作记录
	 * @param unknown $arry
	 */
	public function getUserOperaLogs($arry)
	{
	    foreach ($arry as $key => $value){
	        $condition[$key] = $value;
	    };
        return $this->field(true)->where($condition)->order('id desc')->select();
	}
	
	
	public function getCount($arry)
	{
	    foreach ($arry as $key => $value){
	        $condition[$key] = $value;
	    };
	    return $this->where($condition)->count();
	}
}
?>